<?php
class HomeworksController extends AppController {
    var $name = 'Homeworks';
    var $uses = array('Homework', 'Task', 'StudentProfile');
    
    function add() {
        $this->pageTitle = "提交作业";
        $available_tasks = $this->Task->findAllByClosed(False);
        $arr_for_options = array();
        foreach ($available_tasks as $single_task):
            $arr_for_options[$single_task['Task']['id']] = $single_task['Task']['task_name'];
        endforeach;
        $this->set('ops', $arr_for_options);
        $uploading_student = $this->StudentProfile->findByUserId($this->Auth->user('id'));
        if ($this->Auth->user('role') == 'student') {
            if ($this->data) {
                $this->Task->id = $this->data['Homework']['task_id'];
                $target_path = 'homeworks/'.$this->Task->field('upload_folder').'/'.$this->Auth->user('id').basename($this->data['Homework']['upload_file']['name']);
                if (move_uploaded_file($this->data['Homework']['upload_file']['tmp_name'], $target_path)) {
                    $this->Homework->create();
                    $download_path = '/app/webroot/'.$target_path;
                    $this->Homework->save(array('the_file' => $download_path, 'self_comment' => $this->data['Homework']['self_comment'], 'task_id' => $this->data['Homework']['task_id'], 'user_id' => $this->Auth->user('id'), 'upload_time' => date('Y-m-d H:i:s'), 'student_profile_id' => $uploading_student['StudentProfile']['id']));
                    $this->Session->setFlash("提交成功");
                    $this->redirect(array('controller' => 'student_profiles', 'action' => 'index'));
                }
                else {
                    $this->Session->setFlash("提交失败请重试");
                }
            }
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function view($id = null) {
        $this->Homework->id = $id;
        $this->pageTitle = "查看作业";
        if (($this->Homework->Task->field('closed')) || ($this->Auth->user('id') == $this->Homework->StudentProfile->field('user_id'))) {
            $this->set('data', $this->Homework->read());
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function evaluate($id = null) {
        $this->pageTitle = "评价作业";
        $this->Homework->id = $id;
        $this->set('viewing_data', $this->Homework->read());
        if ($this->Auth->user('role') == 'teacher') {
            if (!$this->data) {
                $this->data = $this->Homework->read();
            }
            else {
                $this->Homework->save($this->data);
                $this->Session->setFlash("作业评价成功");
            }
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function delete($id = null) {
        $this->Homework->id = $id;
        if ($this->Auth->user('id') == $this->Homework->StudentProfile->field('user_id')) {
            $this->Homework->delete($id);
            $this->Session->setFlash("指定作业已被删除");
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function list_for_evaluate($task = null) {
        $this->pageTitle = "作业列表";
        if ($this->Auth->user('role') == 'teacher') {
            $this->set('allhomeworks', $this->Homework->findAllByTaskId($task));
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
}
?>